package org.hubwork.base.common;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

@Component
public class DynamicSqlSessionDaoSupport {   
	  
    private Map<String, SqlSessionFactory> targetSqlSessionFactories;     
  
    private Map<String, SqlSession>  sqlSessions = new HashMap<String, SqlSession>();   
      
    public final SqlSession getSqlSession() { 
    	return sqlSessions.get(ContextSupport.getCurrentSqlSessionFactoryKey());  
    }        
   
    public void setTargetSqlSessionFactories(Map<String, SqlSessionFactory> targetSqlSessionFactories) {   
    	this.targetSqlSessionFactories = targetSqlSessionFactories;
    	for(Map.Entry<String, SqlSessionFactory> entry:this.targetSqlSessionFactories.entrySet()){
    		this.sqlSessions.put(entry.getKey(), new SqlSessionTemplate(entry.getValue())); 
    	}  
    }  
}  
